package com.app.teadoor.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teadoor.bean.BaseListData;
import com.app.teadoor.bean.BaseListDataListBean;
import com.app.teadoor.bean.MenuBean;
import com.app.teadoor.R;
import com.app.teadoor.home.HomeBaseActivity;
import com.app.teadoor.login.LoginActivity;
import com.app.teadoor.other.PdfScanActivity;
import com.app.teadoor.other.WebViewX5Activity;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.MySharedPreferences;
import my.http.MyHttpUtils;


public class UserIndexActivity extends HomeBaseActivity {


    @BindView(R.id.recyclerview)
    RecyclerView listView;

    @BindView(R.id.tv_name)
    TextView textName;
    @BindView(R.id.tv_content)
    TextView textContent;
    @BindView(R.id.img_photo)
    ImageView img_photo;
    MyAdapter mAdapter;

    BaseListDataListBean mListBean;



    @Override
    public void initViews() {
        setContentView(R.layout.user_index_activity);
        super.initViews();
        setTitleBg(R.color.transparent);
        ((TextView) findViewById(R.id.base_title_text)).setTextColor(getResources().getColor(R.color.white));
        setTitle("设置");
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        MySharedPreferences mSp = new MySharedPreferences(this);
        textName.setText("");
        textContent.setText("" + mSp.getUser());
        listView.setLayoutManager(new LinearLayoutManager(mContext));
        initData();
    }


    private void initData() {
        mListBean = new BaseListDataListBean();
        BaseListData data;
        data = new BaseListData("0","关于我们",R.mipmap.icon_about,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("1","参数设置",R.drawable.icon_set,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        data = new BaseListData("2","软件升级",R.mipmap.icon_update,R.drawable.base_img_default_square);
        mListBean.getList().add(data);
        setListView();
    }

    void setListView() {
        if(mAdapter == null){
            mAdapter = new MyAdapter();
            listView.setAdapter(new MyAdapter());
        }else {
            mAdapter.notifyDataSetChanged();
        }
    }



    @Override
    protected void onResume() {
        super.onResume();
//        getImmersionBar().statusBarDarkFont(true,0.5f);
//        getImmersionBar().statusBarDarkFont(true).init();
    }

    @Override
    public void onDoubleClick() {
        super.onDoubleClick();
        if(isFinishing())return;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.btn_exit)
    public void btnExit(){
        logout();
    }




    void logout() {
        new QMUIDialog.MessageDialogBuilder(mContext).setMessage("是否退出APP？").addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        }).addAction(0, "立即退出", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
//                System.exit(0);
                LoginActivity.toLoginAllClear(mContext);
            }
        }).create().show();
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }



    void help() {
        PdfScanActivity.toPdfScanActivity(mContext, "智能秤称重部分使用使用说明.pdf", "用户手册");
    }



    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        public MyAdapter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.base_list_item_and_right_arrow, viewGroup, false);
            return new MyAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                BaseListData data = mListBean.getList().get(position);
                text_name.setText(data.getName());
                img_icon.setImageResource(data.getResid());
                img_icon.setVisibility(View.VISIBLE);
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                BaseListData data = (BaseListData) v.getTag();
                switch (data.getId()){
                    case "2": //软件升级
                        WebViewX5Activity.toWebView(mContext,"http://d.3appstore.com/p1rl","下载");
                        break;
                }
            }
        }
    }


}
